package com.orm.analysis.action;

import com.opensymphony.xwork2.ActionContext;
import com.orm.analysis.model.ContactInfo;
import com.orm.analysis.model.Item;
import com.orm.analysis.model.Supplier;
import com.orm.analysis.service.GenericService;

import java.util.List;
import java.util.Map;


public class SupplierAction extends FrontendAction  {

    private GenericService<Supplier> service;
    private GenericService<Item> itemService;
    private GenericService<ContactInfo> contactInfoService;
    private Supplier supplier;
    private List<ContactInfo> contactInfoList;
    private List<Item> items;
    private List<Supplier> suppliers;


    public SupplierAction(GenericService<Supplier> service, GenericService<Item> itemService,GenericService<ContactInfo> contactInfoService) {
        this.service = service;
        this.itemService = itemService;
        this.contactInfoService = contactInfoService;
    }

    public String getAllSuppliers() {
        suppliers = service.list(page, size);
        return SUCCESS;
    }

    public String getBulkSuppliers() {
                                try {
                                    suppliers = service.findAllWithDepth("items");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return SUCCESS;
                            }


   public String getAllSupplierItems() {
                                try {
                                    suppliers = service.findAllWithDepth("items");

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                return SUCCESS;
                            }

     public String list() {
         suppliers = service.list(page, size);
        return SUCCESS;
    }

    public String setUpForUpdate(){
        prep();
    		if (supplier != null && supplier.getId()   != null) {
                supplier = service.getById (supplier.getId() );
    		}
    		return "success";
    	}
 public String setUpForInsert(){
        prep();

    		return "success";
    	}


        public String insert(){

 //           prep();

        	if(!validationSuccessful()){
        		return "input";
        	}else {
                if (supplier.getId() == null) {
                    service.create(supplier);

                    if (supplier.getItemIdList() != null && supplier.getItemIdList().length > 0) {
                        supplier.setItems(itemService.getByIdList(supplier.getItemIdList()));
                    }

                    service.update(supplier);

                }

            }
            return "success";
        }

public String update(){

            //prep();

        	if(!validationSuccessful()){
        		return "input";
        	}else {

                    if (supplier.getItemIdList() != null && supplier.getItemIdList().length > 0) {
                        supplier.setItems(itemService.getByIdList(supplier.getItemIdList()));
                    }

                    service.update(supplier);



            }
            return "success";
        }


    private void prep() {
        List<Integer> itemIdArrayList = itemService.getIdList();
        Map session = ActionContext.getContext().getSession();
        if (itemIdArrayList != null && itemIdArrayList.size() > 0) {
            session.put("itemIdList", itemIdArrayList.toArray());
        } else {
            session.put("itemIdList", "");
        }

        contactInfoList = contactInfoService.list(page, size);
        session = ActionContext.getContext().getSession();

        if (contactInfoList != null && contactInfoList.size() > 0) {
            session.put("contactInfoList", contactInfoList);
        } else {
            session.put("contactInfoList", "");
        }


    }



    public String delete() {
        service.delete(supplier.getId());
        return SUCCESS;
    }


    private boolean validationSuccessful(){
        if(supplier.getFirstName()==null||supplier.getFirstName().trim().length()<1){
       		addActionMessage("FirstName is required");
       	}
       	if(supplier.getLastName()==null||supplier.getLastName().trim().length()<1){
       		addActionMessage("LastName is required");
       	}

       	if(this.hasActionMessages()){
       		return false;
       	}else{
       		return true;
       	}
      }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public List<Supplier> getSuppliers() {
        return suppliers;
    }

    public void setSuppliers(List<Supplier> suppliers) {
        this.suppliers = suppliers;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }
}
